package DBConnection;

import java.util.List;

import model.entities.MaintenanceStation;

import org.hibernate.HibernateException;

public class MaintenanceStationDB extends HibernateUtil implements
		ConnectDB<MaintenanceStation, Boolean, String> {

	@Override
	public Boolean add(MaintenanceStation maintenanceStation) {
		try {
			if (maintenanceStation != null) {
				this.beginTranSaction();
				this.getCurrentSession().save(maintenanceStation);
				this.commitTransaction();
			}
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
		return true;
	}

	@Override
	public Boolean delete(String ID) {
		MaintenanceStation maintenanceStation = find(ID);
		try {
			if (maintenanceStation != null) {
				this.beginTranSaction();
				this.getCurrentSession().delete(maintenanceStation);
				this.commitTransaction();
			}
		} catch (HibernateException e) {
			this.rollback();
			throw e;
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MaintenanceStation> findAll() {
		try {
			this.beginTranSaction();
			return this.getCurrentSession()
					.createCriteria(MaintenanceStation.class).list();
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}

	@Override
	public Boolean update(MaintenanceStation maintenanceStation) {
		try {
			if (maintenanceStation != null) {
				this.beginTranSaction();
				this.getCurrentSession().update(maintenanceStation);
				this.commitTransaction();
			}
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
		return true;
	}

	@Override
	public MaintenanceStation find(String ID) {
		try {
			this.beginTranSaction();
			return (MaintenanceStation) this.getCurrentSession().get(
					MaintenanceStation.class, ID);
		} catch (Exception e) {
			throw e;
		}
	}
	public static void main(String[] args) {
		List<MaintenanceStation> ll = new MaintenanceStationDB().findAll();
		System.out.println(ll.size());
		System.out.println(new MaintenanceStationDB().find("WS"));
	}

}
